﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SQLite;

namespace ZYO
{
    public static class DataFactory
    {
        private static ZYO.Database.Tables zydb = null;
        public static ZYO.Database.Tables ZYDBXML
        {
            get
            {
                if (null == zydb)
                {
                    zydb = XmlHelper.DeSerialize<ZYO.Database.Tables>(Resources.DefaultValues.zydb);
                }
                return zydb;
            }
        }

        public static void GenerateDatabaseXml(string filepath)
        {
            ZYO.Database.Tables db = new Database.Tables();

            db.Version = 1.0M;
            db.LastUpdateDate = DateTime.Now;

            db.天干Table = new Database.天干[10];
            db.天干Table[0] = new Database.天干();
            db.天干Table[0].枚举值 = Database.enum天干.甲;
            db.天干Table[0].五行属性de枚举值 = Database.enum五行.木;
            db.天干Table[1] = new Database.天干();
            db.天干Table[1].枚举值 = Database.enum天干.乙;
            db.天干Table[1].五行属性de枚举值 = Database.enum五行.木;

            db.地支Table = new Database.地支[12];
            db.地支Table[0] = new Database.地支();
            db.地支Table[0].枚举值 = Database.enum地支.子;
            db.地支Table[0].地蔵天干 = "壬癸";

            db.八卦Table = new Database.九宫八卦[8];
            db.八卦Table[0] = new Database.九宫八卦();
            db.八卦Table[0].枚举值 = Database.enum八卦ID.兑;
            db.八卦Table[0].五行属性de枚举值 = Database.enum五行.水;
            db.八卦Table[0].方向 = "西";
            db.八卦Table[0].下卦地支de枚举值 = Database.enum地支.寅;
            db.八卦Table[0].先天数de枚举值 = Database.enum先天八卦.兑;


            db.甲子Table = new Database.甲子[60];
            db.甲子Table[0] = new Database.甲子();
            db.甲子Table[0].枚举值 = Database.enum甲子.甲子;
            db.甲子Table[0].纳音 = "天河水";

            db.节气Table = new Database.节气[24];
            db.节气Table[0] = new Database.节气();
            db.节气Table[0].iLocation = 1;
            db.节气Table[0].JQBaseTime1996 = new DateTime(1996, 2, 1, 12, 00, 00);
            db.节气Table[0].枚举值 = Database.enum节气.白露;
            db.节气Table[0].上元之局 = 4;
            db.节气Table[0].下元之局 = 7;
            db.节气Table[0].中元之局 = 1;

            db.六十四卦Table = new Database.六十四卦[64];
            db.六十四卦Table[0] = new Database.六十四卦();
            db.六十四卦Table[0].卦象 = "guanxiang";
            db.六十四卦Table[0].枚举值 = Database.enum六十四卦ID.风地观;
            db.六十四卦Table[0].世爻 = 3;
            db.六十四卦Table[0].八卦卦宫 = 3;
            db.六十四卦Table[0].彖曰 = "彖曰";
            db.六十四卦Table[0].象曰 = "象曰";
            db.六十四卦Table[0].周易序号 = Database.enum六十四卦by数.地天泰;

            db.易经爻辞Table = new Database.易经爻辞[64];
            db.易经爻辞Table[0] = new Database.易经爻辞();
            db.易经爻辞Table[0].六十四卦de枚举值 = Database.enum六十四卦ID.地风升;

            db.易经爻辞Table[0].爻辞 = new string[6];
            db.易经爻辞Table[0].爻辞[0] = "111";
            db.易经爻辞Table[0].爻辞[1] = "222";
            db.易经爻辞Table[0].爻辞[2] = "333";
            db.易经爻辞Table[0].爻辞[3] = "444";
            db.易经爻辞Table[0].爻辞[4] = "555";
            db.易经爻辞Table[0].爻辞[5] = "666";

            db.地支旺相Table = new Database.地支旺相[144];
            db.地支旺相Table[0] = new Database.地支旺相();

            db.地支旺相Table[0].月建 = new Database.月建旺相[12];
            db.地支旺相Table[0].月建[0] = new Database.月建旺相();

            db.地支旺相Table[0].地支 = Database.enum地支.亥;
            db.地支旺相Table[0].月建[0].月地支 = Database.enum地支.辰;
            db.地支旺相Table[0].月建[0].旺相值 = Database.e月建旺相.次旺;

            db.字库Table = new Database.字库[8000];
            db.字库Table[0] = new Database.字库();
            db.字库Table[0].字 = "A";
            db.字库Table[0].笔画 = 1;

            db.五行生旺库绝Table = new Database.五行之地支生旺[60];
            db.五行生旺库绝Table[0] = new Database.五行之地支生旺();

            db.五行生旺库绝Table[0].五行 = Database.enum五行.火;
            db.五行生旺库绝Table[0].地支 = new Database.生旺库绝[12];
            db.五行生旺库绝Table[0].地支[0] = new Database.生旺库绝();
            db.五行生旺库绝Table[0].地支[0].地支 = Database.enum地支.亥;
            db.五行生旺库绝Table[0].地支[0].旺相值 = Database.e生旺库绝.绝;

            XmlHelper.Serialize2File(db, filepath);

        }

        private static SQLiteConnection ichingdbConnection = null;
        public static SQLiteConnection ICHINGDBCONNECTION
        {
            get
            {
                if (null == ichingdbConnection)
                {
                    String filename = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) 
                       + "\\database\\iching.sdf";
                    ichingdbConnection = new SQLiteConnection("Data Source=" + filename);
                }
                return ichingdbConnection;
            }
        }

        
        //private static SqlCeConnection myCeConnection = null;

        //private static void InitDBConnection()
        //{
            

        //    //String filename = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) 
        //    //    + "\\database\\iching.sdf";

        //    //Properties.Settings.Default.ConnectionString;
        //    myCeConnection = new SqlCeConnection("Data Source=database\\iching.sdf");

        //    try
        //    {
        //        myCeConnection.Open();
        //    }
        //    catch (Exception e)
        //    {
        //        throw e;
        //    }
        //}

        //private static void CloseDBConnection()
        //{
        //    myCeConnection.Close();
        //}

        //private static DataTable ExecuteQuery(String QueryStringFormat,
        //                                        params String[] QueryParameters)
        //{
        //    DataTable dtReturn = new DataTable();

        //    if (null == myCeConnection || myCeConnection.State != ConnectionState.Open)
        //    {
        //        InitDBConnection();
        //    }

        //    SqlCeCommand myCommand = new SqlCeCommand();
        //    myCommand.CommandType = CommandType.Text;
        //    myCommand.CommandText = String.Format(QueryStringFormat, QueryParameters);
        //    myCommand.Connection = myCeConnection;

        //    SqlCeDataAdapter myDataAdapter = new SqlCeDataAdapter(myCommand);
        //    myDataAdapter.Fill(dtReturn);

        //    return dtReturn;
        //}

        //public static DataRow GetSingleRowOrNull(String tablename, String[] Columns, String Filter)
        //{
        //    DataRow row = null;

        //    String selectColumns = String.Empty;
        //    foreach (String col in Columns)
        //    {
        //        if (!String.IsNullOrEmpty(selectColumns))
        //        {
        //            selectColumns += ",";
        //        }
        //        selectColumns += col;
        //    }

        //    String queryString = String.Format("Select {0} from {1} Where {2}", selectColumns, tablename, Filter);
        //    DataTable dtReturn = ExecuteQuery(queryString);

        //    // no rows returned
        //    if (dtReturn.Rows.Count == 0)
        //    {
        //        row = null;
        //    }
        //    else
        //    {
        //        // Raise Exceptions if not unique row returned
        //        if (dtReturn.Rows.Count != 1)
        //        {
        //            ZyoException ex = new ZyoException("Data Error! not unique rows retured,Rows.Count=" + dtReturn.Rows.Count.ToString());
        //            throw ex;
        //        }
        //        else // unique row retured
        //        {
        //            row = dtReturn.Rows[0];
        //        }
        //    }

        //    return row;
        //}
        //public static DataRow GetSingleRow(String tablename, String[] Columns, String Filter)
        //{
        //    DataRow row = GetSingleRowOrNull(tablename, Columns, Filter);
        //    // Raise Exceptions if not unique row returned
        //    if (null == row)
        //    {
        //        ZyoException ex = new ZyoException(string.Format(
        //            "Data Error! no rows retured! Table={0},Filter={1}",
        //            tablename, Filter));
        //        throw ex;
        //    }

        //    return row;
        //}
    }
}

